<?php

class Admin_CategoriesController extends Zend_Controller_Action {

    public function indexAction() {

    }

    public function listAction() {
        $categories = new Content_Model_DbTable_Categories();
        $res = $categories->getList();
        $this->_helper->json($res->toArray());
    }

    public function singleAction() {
        $categories = new Content_Model_DbTable_Categories();
        $res = $categories->find($this->_getParam('id'))->current();
        $this->_helper->json($res->toArray());
    }

    public function saveAction() {
        $data = (array) $this->_getParam('data');
        $id = (int) $data['id'];
        unset($data['id']);
        $categories = new Content_Model_DbTable_Categories();
        $row = ($id === -1) ? $categories->fetchNewChild($data['parent']) : $categories->find($id)->current();
        $row->setFromArray($data);
        $res = $row->save();
        $this->_helper->json($res);
    }

    public function removeAction() {
        $id = (int) $this->_getParam('id');
        $categories = new Content_Model_DbTable_Categories();
        $row = $categories->find($id)->current();
        $res = $row->delete();
        $this->_helper->json($res);
    }

    public function moveAction() {
        $id = $this->_getParam('id');
        $direction = $this->_getParam('direction');
        
        $categories = new Content_Model_DbTable_Categories();
        if ('up' === $direction) {
            $res = $categories->moveUp($id);
        }
        if ('down' === $direction) {
            $res = $categories->moveDown($id);
        }
        $this->_helper->json($res);
    }
}
